package com.ktjiaoyu.server.controller;

import com.ktjiaoyu.server.pojo.RespBean;
import com.ktjiaoyu.server.service.IWorkFlowService;
import com.ktjiaoyu.server.vo.WorkFlowVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/workFlow")
public class WorkFlowController {
    @Resource
    IWorkFlowService workFlowService;

    //查询流程部署记录
    @ApiOperation("查询流程部署记录")
    @GetMapping("/loadAllDeployment")
    public RespBean loadAllDeployement(WorkFlowVo workFlowVo){
        return workFlowService.queryProcessDeploy(workFlowVo);
    }

    //查询流程定义记录
    @ApiOperation("查询流程定义记录")
    @GetMapping("/loadAllProcessDefinition")
    public RespBean loadAllProcessDefinition(WorkFlowVo workFlowVo){
        return workFlowService.loadAllProcessDefinition(workFlowVo);
    }

    @PostMapping("/addWorkFlow")
    @ApiOperation("添加流程部署")
    public RespBean addWorkFlow(WorkFlowVo workFlowVo) {
        try {
            workFlowService.addWorkFlow(workFlowVo.getUploadFile().getInputStream(), workFlowVo.getDeploymentName());
            return RespBean.success("流程部署成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return RespBean.error("流程部署失败!");
        }
    }

    @GetMapping("/findProcessImage/{deploymentId}")
    @ApiOperation("查看流程图")
    public void findProcessImage(@PathVariable String deploymentId,
                                 HttpServletResponse response){
        InputStream inputStream = workFlowService
                .queryProcessDeploymentImage(deploymentId);

        ServletOutputStream outputStream=null;
        try {
            outputStream = response.getOutputStream();
            byte[] b=new byte[1024];
            int len=0;
            while((len=inputStream.read(b))>0){
                outputStream.write(b,0,b.length);
            }
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                outputStream.close();
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @GetMapping("/startProcess/{id}")
    @ApiOperation("提交申请")
    public RespBean startProcess(@PathVariable Integer id){
        try {
            workFlowService.startProcess(id);
            return RespBean.success("提交申请成功");
        } catch (Exception e) {
            e.printStackTrace();
            return RespBean.error("提交申请失败");
        }
    }

    @GetMapping("/loadCurrentUserTask")
    @ApiOperation("查询我的待办任务")
    public RespBean loadCurrentUserTask(WorkFlowVo workFlowVo){
        return workFlowService.queryCurrentUserTask(workFlowVo);
    }


    /**
     * 通过流程部署ID删除流程部署信息
     */
    @GetMapping("/deleteWorkFlow/{deploymentId}")
    @ApiOperation("通过流程部署ID删除流程部署信息")
    public RespBean deleteWorkFlow(@PathVariable("deploymentId")String deploymentId) {
        try {
            workFlowService.deleteWorkFlow(deploymentId);
            return RespBean.success("删除流程部署信息成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return RespBean.error("删除流程部署信息失败!");
        }
    }

    /**
     * 通过流程部署ID集合，批量删除流程部署及流程定义信息
     */
    @GetMapping("/batchDeleteWorkFlow")
    @ApiOperation("通过流程部署ID集合，批量删除流程部署及流程定义信息")
    public RespBean batchDeleteWorkFlow(WorkFlowVo workFlowVo) {
        try {
            String [] deployments = workFlowVo.getIds();
            for (String deploymentId : deployments) {
                this.workFlowService.deleteWorkFlow(deploymentId);
            }
            return RespBean.success("批量删除流程部署及流程定义信息成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return RespBean.error("批量删除流程部署及流程定义信息失败!");
        }
    }

    /**
     * 查询请假单数据
     * @return
     */
    @GetMapping("/queryLeaveBillByTaskId/{taskId}")
    @ApiOperation("根据任务id查询请假单数据")
    public RespBean queryLeaveBillByTaskId(@PathVariable String taskId){
        return workFlowService.queryLeaveBillByTaskId(taskId);
    }

    @GetMapping("/queryOutcomeByTaskId/{taskId}")
    @ApiOperation("根据任务id查询连线")
    public RespBean queryOutcomeByTaskId(@PathVariable String taskId){
        return workFlowService.queryOutcomeByTaskId(taskId);
    }

    @GetMapping("/loadAllComments/{taskId}")
    @ApiOperation("根据任务id查询批注")
    public RespBean queryCommentByTaskId(@PathVariable String taskId){
        return workFlowService.queryCommentByTaskId(taskId);
    }

    @ApiOperation("完成任务")
    @PostMapping("/completeTask")
    public RespBean completeTask(@RequestBody WorkFlowVo workFlowVo){
        try {
            workFlowService.completeTask(workFlowVo);
            return RespBean.success("完成任务成功!");
        } catch (Exception e) {
            e.printStackTrace();
            return RespBean.error("完成任务失败!");
        }
    }
}
